package cn.mumzone.pos.presenter;

import android.text.TextUtils;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.util.PatternUtils;
import cn.mumzone.pos.model.MemberEntity;
import cn.mumzone.pos.model.source.MemberRepository;
import cn.mumzone.pos.presenter.contract.MemberAddOrModifyContract;

public class MemberAddOrModifyPresenter extends AbsPresenter<MemberAddOrModifyContract.View, MemberRepository> implements MemberAddOrModifyContract.Presenter {


    @Override
    public boolean isInputLegal(String name, String phone, String selectedLevel) {
        if (TextUtils.isEmpty(name)) {
            mView.showError("请输入会员姓名");
            return false;
        }

        if (!PatternUtils.checkName(name)) {
            mView.showError("会员姓名不能输入特殊字符");
            return false;
        }

        if (!PatternUtils.isMobileNO(phone)) {
            mView.showError("手机号格式不正确");
            return false;
        }

        if (TextUtils.isEmpty(selectedLevel)) {
            mView.showError("请选择会员等级");
            return false;
        }

        return true;
    }

    @Override
    public void createOrModifyMemberInfo(String name, String phone, String selectedGender, String level,
                                         String birthday,
                                         String relation, String status, String dueDate, final String memberId) {
        ApiCallBack<MemberEntity> callBack = new ApiCallBack<MemberEntity>() {
            @Override
            public void onSuccess(MemberEntity memberEntity) {

                if (TextUtils.isEmpty(memberId)) {
                    mView.onCreateOrModifyMemberInfoSuccess(memberEntity, true);
                } else {
                    mView.onCreateOrModifyMemberInfoSuccess(memberEntity, false);
                }

            }

            @Override
            public void onError(BaseResponse response) {
                if (TextUtils.isEmpty(memberId)) {
                    mView.onCreateOrModifyMemberInfoFailed(true,response.getMsg());
                } else {
                    mView.onCreateOrModifyMemberInfoFailed(false,response.getMsg());
                }
            }
        };
        mRepo.createNewMember(name, phone, selectedGender, level, birthday, relation, status, dueDate,
                memberId, callBack);
        addSubscrebe(callBack);
    }
}
